<template>
  <div class="search-page">
    <group class="filter-group">
      <div class="search-content">
        <div class="filter" @click="showPicker = !showPicker">
          分类器<x-icon type="ios-arrow-down" size="14"></x-icon>
        </div>
        <CommonSearch
          ref="commonSearch"
          placeholder="名称、ID或使用人"
          @searchFun="searchFun"
        ></CommonSearch>
        <x-button class="search-btn" size="mini" @click.native="resetList">搜索</x-button>
        <x-button class="rest-btn" size="mini" @click.native="clearSearch">重置</x-button>
      </div>
      <template v-if="showPicker">
        <popup-picker
          :key="1"
          title="资产分类"
          :data="classList"
          :columns="2"
          @on-change="changeClass"
          v-model="classId"
          show-name
          placeholder="请选择资产分类"
        ></popup-picker>
        <popup-picker
          :key="2"
          title="品牌型号"
          :data="tagList"
          v-model="tags"
          placeholder="请选择品牌型号"
        ></popup-picker>
        <popup-picker
          :key="3"
          title="位置"
          :data="placeList"
          v-model="place"
          placeholder="请选择位置"
        ></popup-picker>
      </template>
    </group>
    <div class="asset-list">
      <MeScroll
        ref="createMe"
        meScrollRef="createMe"
        @getList="getList"
        @resetList="resetList"
        :height="`calc(100vh - ${showPicker ? '200px' : '50px'})`"
      >
        <div class="list" slot="list">
          <template v-if="list.length > 0">
            <div
              class="item"
              v-for="(item, index) in list"
              :key="index"
              @click="toLog(item.assetId)"
            >
              <div class="title">
                <span :class="`tag-${item.borrowState} tag`">{{
                  getStatus(item.borrowState)
                }}</span>
                <span class="name">{{ item.assetName }}</span>
                <!-- <ion-icon name="chevron-forward-outline"></ion-icon> -->
                <x-icon class="more" type="ios-arrow-right"></x-icon>
              </div>
              <!-- <div class="label">
                <span class="title">资产数字ID</span>
                <span class="name">{{ item.assetCode }}</span>
              </div> -->
              <!-- <div class="label">
                <span class="title">资产分类</span>
                <span class="name">{{ item.className }}</span>
              </div> -->
              <div class="label">
                <span class="title">使用人</span>
                <span class="name">{{ item.useName }}</span>
              </div>
              <div class="label">
                <span class="title">品牌型号</span>
                <span class="name">{{ item.tags }}</span>
              </div>
            </div>
          </template>
          <template v-else><no-data></no-data></template>
        </div>
      </MeScroll>
    </div>
  </div>
</template>

<script>
import {
  Group,
  XInput,
  Tab,
  TabItem,
  Scroller,
  LoadMore,
  PopupPicker,
  XButton,
} from "vux";
import MeScroll from "@/components/common/me-scroll";
import AssetApi from "@/api/asset.js";
import CommonSearch from "../components/search";
export default {
  components: {
    XButton,
    MeScroll,
    Group,
    XInput,
    CommonSearch,
    Scroller,
    LoadMore,
    PopupPicker,
    Tab,
    TabItem,
  },
  data() {
    return {
      showPicker: false,
      classId: [],
      classList: [],
      tags: [],
      tagList: [[]],
      place: [],
      placeList: [[]],
      list: [],
      load: false,
    };
  },
  mounted() {
    this.assetClass();
    this.findTenPlace();
    this.getList(1, 5);
  },
  methods: {
    getList(num, size) {
      console.log(139,this.classId)
      let _params = {
        key: this.name,
        classId: this.classId.length>1?this.classId[1]:"",
        place: this.place.toString(),
        tags: this.tags.toString(),
        page: num,
        pageSize: size,
      };
      AssetApi.getAssetList(_params)
        .then((res) => {
          if (res.code == "0000") {
            this.$refs.createMe.mescroll.endSuccess(res.data.records.length);
            this.list = [...this.list, ...res.data.records];
          }
        })
        .finally((res) => {
          this.load = true;
        });
    },
    resetList() {
        // this.$refs.commonSearch
      let _params = {
        key: this.$refs.commonSearch.name,
        classId: this.classId.length>1?this.classId[1]:"",
        place: this.place.toString(),
        tags: this.tags.toString(),
        page: 1,
        pageSize: 5,
      };
      AssetApi.getAssetList(_params)
        .then((res) => {
          if (res.code == "0000") {
            this.$refs.createMe.mescroll.endSuccess(res.data.records.length);
            this.list = [...res.data.records];
          }
        })
        .finally((res) => {
          this.load = true;
        });
    },
    searchFun(name) {
        console.log(name)
      this.load = false;
      this.name = name;
      this.page = 1;
      this.list = [];
      this.resetList(1, 5);
      // 根据名称执行搜索操作
    },
    clearSearch() {
      this.load = false;
      this.name = name;
      this.classId = [];
      this.tags = [];
      this.place = [];
      this.page = 1;
      this.list = [];
      this.resetList(1, 5);
    },
    toLog(id) {
      this.$router.push({
        path: "log",
        query: {
          id: id,
          edit: true,
        },
      });
    },
    getStatus(state) {
      let text = "未出借";
      switch (state) {
        case 0:
          text = "未出借";
          break;
        case 1:
          text = "出借待确认";
          break;
        case 2:
          text = "出借中";
          break;
        case 3:
          text = "归还待确认";
          break;
        default:
          text = "未出借";
          break;
      }
      return text;
    },
    assetClass() {
      let _params = {
        pageNum: 1,
        pageSize: 1000,
      };
      AssetApi.assetClassTree(_params).then((res) => {
        if (res.code == "0000") {
          let _classList = [];
          let resData = res.data;
          for (let i in resData) {
            let temp = {
              name: resData[i].name,
              value: resData[i].id,
              parent: 0,
            };
            _classList.push(temp);
            for (let j in resData[i].children) {
              let temp1 = {
                name: resData[i].children[j].name,
                value: resData[i].children[j].id,
                parent: resData[i].id,
              };
              _classList.push(temp1);
            }
          }
          this.classList = _classList;
        }
      });
    },
    changeClass(val) {
      if (val.length > 0) {
        let _params = {
          classId: val[1]?val[1]:val[0],
        };
        AssetApi.getTagList(_params).then((res) => {
          if (res.code == "0000") {
            this.tagList = [res.data] || [[]];
            this.tags = [];
          }
        });
      }
    },
    findTenPlace() {
      AssetApi.findTenPlace().then((res) => {
        if (res.code == "0000") {
          this.placeList = [res.data] || [];
        }
      });
    },
  },
};
</script>
<style lang="less">
.search-page {
  overscroll-behavior-y: contain;
  .mescroll {
    -webkit-overflow-scrolling: touch;
    overflow: auto;
    height: 700px;
  }
}
</style>
<style lang="less" scoped>
.search-page {
  .search-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 15px 0 20px;
    height: 50px;
    line-height: 50px;

    .filter {
      color: #2652c6;
      width: 76px;
      font-size: 13px;
    }
    .search-btn {
      color: #fff;
      background: #2652c6;
      border-radius: 20px;
      font-size: 12px;
      width: 60px;
      height: 32px;
      line-height: 32px;
      text-align: center;
      padding: 0 10px;
    }
    .rest-btn {
      color: #2652c6;
      background: #fff;
      border: 1px solid #2652c6;
      border-radius: 20px;
      font-size: 12px;
      width: 60px;
      height: 32px;
      line-height: 32px;
      text-align: center;
      margin-left: 5px;
      margin-top: 0;
      padding: 0 10px;
    }
  }
}
.list {
  // height: auto;
  overflow-y: scroll;
  background: #f1f1f1;
  padding: 10px;
  -webkit-overflow-scrolling: touch;
  // overscroll-behavior-y: contain;
  .item {
    padding: 10px 15px;
    border-radius: 8px;
    background: #fff;
    margin-bottom: 10px;

    .title {
      height: 30px;
      line-height: 30px;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      position: relative;
      .tag {
        color: #fff;
        position: relative;
        display: inline-block;
        padding: 0 3px 0 6px;
        font-size: 12px;
        line-height: 20px;
        height: 20px;
        border-radius: 4px 0 0 4px;
        &::after {
          border-left: 10px solid transparent;
          border-right: 10px solid transparent;
          //   border-top: 8px solid red;
          content: "";
          position: absolute;
          top: 6px;
          right: -14px;
          width: 0;
          z-index: 99;
          -webkit-transform: rotate(270deg);
          transform: rotate(270deg);
        }
      }
      .tag-1,
      .tag-0 {
        background: rgb(87, 184, 135);
        &::after {
          border-top: 8px solid rgb(87, 184, 135);
        }
      }
      .tag-2 {
        background: rgb(53, 120, 230);
        &::after {
          border-top: 8px solid rgb(53, 120, 230);
        }
      }
      .tag-3 {
        background: rgb(240, 153, 82);
        &::after {
          border-top: 8px solid rgb(240, 153, 82);
        }
      }
      .name {
        display: inline-block;
        margin-left: 20px;
      }
      .more {
        position: absolute;
        right: 0px;
      }
    }

    .label {
      display: flex;
      justify-content: flex-start;
      height: 30px;
      line-height: 30px;
      color: rgb(127, 127, 127);

      .title {
        display: inline-block;
        width: 100px;
      }
      .name {
        display: inline-block;
        flex: 1;
        /* 单行显示省略号 */
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
  }
}
.load-complete {
  text-align: center;
  height: 40px;
  line-height: 40px;
  color: #666;
}
.filter-group {
  margin-top: 10px;
}
</style>
